package com.smallkingson.ylib.core.utils;

import com.smallkingson.ylib.core.Yr;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

public class YFiles {

    public static void delete(File dir) {

        if (!dir.exists()) {
            try {
                throw new Exception("目录不存在： " + dir);
            } catch (Exception e) {
                Yr.logError(e);
            }
        }
        String[] list = dir.list();

        if (list == null) {
            return;
        }

        for (String s : list) {

            File file = new File(dir.toString() + "/" + s);

            deleteFile(file);
        }
    }

    public static void deleteFile(File file) {

        while (file.exists()) {
            file.delete();
        }
    }

    public static void traverse(String path, Handle handle) {

        File file = new File(path);
        handle.handle(file);
        if (file.isDirectory()) {

            File[] list = file.listFiles();

            for (int i = 0; i < list.length; i++) {

                File p = list[i];

                traverse(p.toString(), handle);
            }
        }
    }
    /**
     *
     * @param str
     * @param file
     */
    public static void write(String str, File file) {
        write(str, file, false);
    }
    /**
     *
     * @param str
     * @param file
     */
    public static void write(String str, File file,boolean append) {

        OutputStream out = null;

        BufferedWriter bw = null;
        try {
            File parentFile = file.getParentFile();
            if (!parentFile.exists()) {
                parentFile.mkdirs();
            }
            out = new FileOutputStream(file,append);

            bw = new BufferedWriter(new OutputStreamWriter(out, "UTF-8"));

            bw.write(str);

            bw.close();
        } catch (Exception e) {
            Yr.logError(e);
        }finally {
            try {
                bw.close();
            } catch (IOException e) {
                Yr.logError(e);
            }
        }
    }
    public static boolean copyFile(File formFile, File toFile) {

        InputStream in = null;
        try {
            in = new FileInputStream(formFile);
            return copyFile(in,toFile);
        } catch (Exception e) {
            Yr.logError(e);
        } finally {
            try {
                in.close();
            } catch (Exception e2) {
                Yr.logError(e2);
            }
        }
        return false;
    }
    public static boolean copyFile(InputStream in, File toFile) {

        OutputStream out = null;
        try {
            out = new FileOutputStream(toFile);

            byte[] buf = new byte[1024];

            int len = 0;

            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            return true;
        } catch (Exception e) {
            Yr.logError(e);
        } finally {
            try {
                out.close();
            } catch (Exception e2) {
                Yr.logError(e2);
            }
        }
        return false;
    }
    public static void cut(File from,File to){
        copyFile(from,to);
        deleteFile(from);
    }

    public static String getString(InputStream in) throws IOException {

        String line=null;

        BufferedReader br = new BufferedReader(new InputStreamReader(in));

        StringBuilder stringBuilder=new StringBuilder();

        while ((line=br.readLine())!=null){
            stringBuilder.append(line);
        }

        return stringBuilder.toString();
    }

    public static String readString(String path) {

        return readString(new File(path));
    }

    /**
     *
     * @param file
     * @return
     */
    public static String readString(File file) {

        BufferedReader br = null;
        try {
            Yr.e("读取文件：" + file.toString());

            br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));

            String line;

            StringBuilder sb = new StringBuilder();

            while ((line = br.readLine()) != null) {
                sb.append(line);
                if (sb.length() > 1942998) {
                    break;
                }
            }

            br.close();

            return sb.toString();

        } catch (Exception e) {
            Yr.logError(e);
            Yr.e("文件读取出错，可能不存在");
        }finally {
            try {
                br.close();
            } catch (IOException e) {
                Yr.logError(e);
            }
        }
        return null;
    }

    public static interface Handle {
        void handle(File file);
    }
}
